package com.winit.jumpServer.web;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.base.beans.LoginUser;
import com.winit.common.base.web.BaseController;
import com.winit.common.filter.UserInfoUtil;
import com.winit.common.util.Constants;
import com.winit.core.server.factory.LinuxJumpServerFactory;
import com.winit.jumpServer.bean.JumpServerInfo;
import com.winit.jumpServer.manager.JumpServerManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 跳板机相关
 * Created by wangjian on 16/4/5.
 */
@RestController
@RequestMapping("/jumpServer")
public class JumpServerController extends BaseController {

    static final Logger logger = LoggerFactory.getLogger(JumpServerController.class);

    @Autowired
    private JumpServerManager jumpServerManager;

    /**
     * 获取跳板机列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getJumpServerList")
    @ResponseBody
    public Map<String, Object> getJumpServerList(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        logger.info("获取跳板机列表开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("获取跳板机列表,获取用户信息为空");
            return this.noUserRtn();
        }
        List<JSONObject> list = jumpServerManager.getJumpServerList();
        rtnMap.put("code", "0");
        JSONObject data = new JSONObject();
        data.put("count", list.size());
        data.put("list", list);
        return this.getRtnMap(rtnMap, userInfo, data);
    }

    /**
     * 获取跳板机信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/getJumpServerInfo")
    @ResponseBody
    public Map<String, Object> getJumpServerInfo(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<String, Object>();
        logger.info("获取跳板机信息开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("获取跳板机信息,获取用户信息为空");
            return this.noUserRtn();
        }
        String ip = request.getParameter("ip");
        if (StringUtils.isEmpty(ip)) {
            logger.info("获取跳板机信息,跳板机ip为空");
            return this.paramEmptyRtn(userInfo, "ip");
        }

        JumpServerInfo info = jumpServerManager.getJumpServerInfo(ip);
        if (info == null) {
            logger.info("获取跳板机信息失败,ip=" + ip);
            rtnMap.put("code", "1");
            rtnMap.put("desc", "查询失败");
            return this.getRtnMap(rtnMap, userInfo, new JSONObject());
        }
        rtnMap.put("code", "0");
        return this.getRtnMap(rtnMap, userInfo, (JSONObject) JSONObject.toJSON(info));
    }

    /**
     * 修改跳板机信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/editJumpServerInfo")
    @ResponseBody
    public Map<String, Object> editJumpServerInfo(HttpServletRequest request) {
        logger.info("修改跳板机信息开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("修改跳板机信息,获取用户信息为空");
            return this.noUserRtn();
        }
        String type = request.getParameter("type");
        if (StringUtils.isEmpty(type)) {
            logger.info("修改跳板机信息,操作类型type为空");
            return this.paramEmptyRtn(userInfo, "type");
        }
        String ip = request.getParameter("ip");
        if (StringUtils.isEmpty(ip)) {
            logger.info("修改跳板机信息,跳板机ip为空");
            return this.paramEmptyRtn(userInfo, "ip");
        }

        String sshPort = request.getParameter("sshPort");
        String level = request.getParameter("level");
        String parentIp = request.getParameter("parentIp");
        String name = request.getParameter("name");
        String desc = request.getParameter("desc");
        if ("1".equals(type) || "2".equals(type)) {
            if (StringUtils.isEmpty(sshPort)) {
                logger.info("修改跳板机信息,跳板机sshPort为空");
                return this.paramEmptyRtn(userInfo, "sshPort");
            }
//            if(StringUtils.isEmpty(list)){
//                logger.info("修改跳板机信息,跳板机list为空");
//                return this.paramEmptyRtn(userInfo,"list");
//            }
            if (StringUtils.isEmpty(level)) {
                logger.info("修改跳板机信息,跳板机level为空");
                return this.paramEmptyRtn(userInfo, "跳板机级别");
            }
            if (!Constants.JUMPSERVER_LEVELL_1.equals(level)) {
                if (StringUtils.isEmpty(parentIp)) {
                    logger.info("修改跳板机信息,跳板机parentIp为空");
                    return this.paramEmptyRtn(userInfo, "上级跳板机IP");
                }
            }
        }

        Map<String, Object> rtnMap = jumpServerManager.editJumpServerInfo(type, ip, sshPort, "", level, parentIp, name, desc);
        return this.getRtnMap(rtnMap, userInfo, new JSONObject());
    }

    /**
     * 初始化跳板机,使beetle机器可免密访问跳板机
     *
     * @param request
     * @return
     */
    @RequestMapping("/initJumpServer")
    @ResponseBody
    public Map<String, Object> initJumpServer(HttpServletRequest request) {
        logger.info("初始化跳板机开始");
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("初始化跳板机,获取用户信息为空");
            return this.noUserRtn();
        }

        String ip = request.getParameter("ip");
        if (StringUtils.isEmpty(ip)) {
            logger.info("初始化跳板机,跳板机ip为空");
            return this.paramEmptyRtn(userInfo, "ip");
        }

        String sshPort = request.getParameter("sshPort");
        if (StringUtils.isEmpty(sshPort)) {
            logger.info("初始化跳板机,跳板机sshPort为空");
            return this.paramEmptyRtn(userInfo, "sshPort");
        }
        String sshUser = request.getParameter("sshUser");
        if (StringUtils.isEmpty(sshUser)) {
            logger.info("初始化跳板机,操作类型sshUser为空");
            return this.paramEmptyRtn(userInfo, "sshUser");
        }
        String pass = request.getParameter("pass");
        if (StringUtils.isEmpty(pass)) {
            logger.info("初始化跳板机,操作类型pass为空");
            return this.paramEmptyRtn(userInfo, "pass");
        }
        String rootPass = request.getParameter("rootPass");
        if (StringUtils.isEmpty(rootPass)) {
            logger.info("初始化跳板机,操作类型rootPass为空");
            return this.paramEmptyRtn(userInfo, "rootPass");
        }
        String level = request.getParameter("level");
        if (StringUtils.isEmpty(level)) {
            logger.info("初始化跳板机,操作类型level为空");
            return this.paramEmptyRtn(userInfo, "跳板机等级");
        }
        String parentIp = request.getParameter("parentIp");
        if (!Constants.JUMPSERVER_LEVELL_1.equals(level)) {
            if (StringUtils.isEmpty(parentIp)) {
                logger.info("初始化跳板机,操作类型parentIp为空");
                return this.paramEmptyRtn(userInfo, "上级跳板机IP");

            }
        }
        Map<String, Object> rtnMap = jumpServerManager.initJumpServer(ip, sshPort, sshUser, pass, rootPass, level, parentIp);
        logger.info("初始化跳板机,ip=" + ip + ",level=" + level + ",parentIp=" + parentIp + ",返回:" + rtnMap);
        return this.getRtnMap(rtnMap, userInfo, new JSONObject());
    }

    @RequestMapping("/reloadJumpSSHServer")
    @ResponseBody
    public Map<String, Object> reloadJumpSSHServer(HttpServletRequest request) {
        Map<String, Object> rtnMap = new HashMap<>();
        LoginUser userInfo = UserInfoUtil.getUserInfo(request);
        if (userInfo == null) {
            logger.info("重置跳板机对象,获取用户信息为空");
            return this.noUserRtn();
        }

        try {
            LinuxJumpServerFactory.getInstance().reloadJumpServer();
        } catch (Exception e) {
            rtnMap.put("code", "1");
            rtnMap.put("desc", "重置跳板机对象异常." + e.getLocalizedMessage());
            logger.error("重置跳板机对象异常.", e);
            return getRtnMap(rtnMap, userInfo, new JSONObject());
        }
        rtnMap.put("code", "0");
        return getRtnMap(rtnMap, userInfo, new JSONObject());
    }

}
